package com.su.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.su.common.BaseContext;
import com.su.common.R;
import com.su.pojo.ShoppingCart;
import com.su.pojo.User;
import com.su.service.ShoppingCartService;
import com.su.service.UserService;
import com.su.utils.SMSUtils;
import com.su.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 用户
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户登录验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user) {
        String phone = user.getPhone();
        if (StringUtils.isNotEmpty(phone)) {
            //生成验证码
            String code = ValidateCodeUtils.generateValidateCode4String(4);
            log.info("系统生成的验证码: {}", code);
            //将验证码存到session中
           // session.setAttribute("code", code);
            //将验证码存到redis,存活时间5分钟
            redisTemplate.opsForValue().set(phone,code,5L, TimeUnit.MINUTES);
            //发送短信
            //SMSUtils.sendMessage("阿里云短信测试","SMS_154950909","15237601597",code);
            return R.success("发送成功");
        }
        return R.error("请输入手机号");
    }

    @PostMapping("/login")
    public R<User> userLogin(@RequestBody Map map, HttpSession session) {
        //获取用户输入的信息
        String phone =map.get("phone").toString();
        String code = map.get("code").toString();
        log.info("用户输入的验证码：{}",code);

        //redis中存的验证码
        String redisCode = (String) redisTemplate.opsForValue().get(phone);
        if (redisCode != null && redisCode.equals(code)) {
            //登录成功
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getPhone, phone);
            User user = userService.getOne(userLambdaQueryWrapper);
            if (user == null) {
                //新用户，自动注册
                user = new User();
                user.setPhone(phone);
                userService.save(user);
            }
            //将用户信息存入session
            session.setAttribute("user",user.getId());
            //删除验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }
        //验证码错误
        return R.error("验证码错误");
    }

    /**
     * 用户退出
     * @param session
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        //清除session
        session.removeAttribute("user");
        return R.success("退出成功");
    }
}
